package cn.sheep.dolphin.common

import cn.sheep.dolphin.config.DolphinConfig
import org.apache.spark.{SparkConf, SparkContext}
import cn.sheep.dolphin.common.DolphinEnum._
import org.apache.spark.serializer.KryoSerializer

/**
  * author: old sheep
  * QQ: 64341393
  * Created 2018/11/28
  */
object DolphinAppComm {

	/**
	  * 封装创建sparkContext实例
	  *
	  * @param appName
	  * @param params
	  * @return
	  */
	def createSparkContext(appName: String, params: Map[String, String] = Map.empty) = {
		val sparkConf = new SparkConf()
		sparkConf.setAppName(appName)
		sparkConf.set("spark.serializer", classOf[KryoSerializer].getName)

		if (DolphinConfig._power.equals(local.toString)) { // 运行模式的开关
			sparkConf.setMaster("local[*]")
		}


		// 封装用户传递进来的参数
		params.foreach { case (key, value) => sparkConf.set(key, value) }

		new SparkContext(sparkConf)
	}


}
